using System;
using System.Security.Authentication;
using System.Security.Principal;
using System.Threading;
using Metro.SmartClient.Infrastructure.Interface.Services;
using Metro.SmartClient.Infrastructure.Library.Properties;   
using Microsoft.Practices.CompositeUI;
using Microsoft.Practices.CompositeUI.Services;
using Microsoft.Practices.ObjectBuilder;
using Metro.Admin.BusinessEntities;
using Microsoft.Practices.CompositeUI.EventBroker;
using Metro.SmartClient.Infrastructure.Interface;


namespace Metro.SmartClient.Infrastructure.Library.Services
{
	public class SimpleWinFormAuthenticationService : IAuthenticationService
	{
        private IUserSelectorService userSelectorService;
        private WorkItem localWorkitem;
        

        [ServiceDependency]
        public WorkItem WorkItem
        {
            get { return localWorkitem; }
            set { localWorkitem = value; }
        }
		
		[InjectionConstructor]
        public SimpleWinFormAuthenticationService([ServiceDependency] IUserSelectorService userSelector, [ServiceDependency] WorkItem workItem)
		{
            this.userSelectorService = userSelector;
            this.localWorkitem = workItem;
            
		}

		public void Authenticate()
		{
            User user = userSelectorService.SelectUser();
           
			if (user != null)
			{
                WorkItem.Items.Add(user, "UserSession");
				GenericIdentity identity = new GenericIdentity(user.Name);
				GenericPrincipal principal = new GenericPrincipal(identity, user.Roles);
				Thread.CurrentPrincipal = principal;
			}
            //else
            //{
            //    throw new AuthenticationException(Resources.NoUserProvidedForAuthentication);
            //}
		}
	}
}
